<?php

namespace app\circle\model;

use Overtrue\Pinyin\Pinyin;
use think\Cookie;

class MemberModel extends BaseModel
{
    protected $name = 'circle_member'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    public function setMemberPassAttr($value, $data)
    {
        return md5($value);
    }

    public function getCompanyInfoAttr($value, $data)
    {
        $model = new CompanyModel();
        $companyInfo = $model->where('company_id', $data['company_id'])->find();
        $companyInfo['address_text'] = $companyInfo->address_text;
        return $companyInfo;
    }

    public function getThemeListAttr($value, $data)
    {
        $model = new ThemeModel();
        return $model->where('member_id', $data['member_id'])->order('create_time desc')->limit(5)->select();
    }

    /**
     * 绑定企业
     * @param $data
     * @param $memberId
     */
    public function bindCompany($data, $memberId)
    {
        if (empty($memberId)) {
            $this->error = '请先登陆';
            return false;
        }
        $meetingCompanyModel = new MeetingCompanyModel();
        $meetingCompanyInfo = $meetingCompanyModel->where('meeting_id', $data['meeting_id'])->where('company_code', $data['company_code'])->find();
        if (empty($meetingCompanyInfo)) {
            $this->error = '您输入的企业码有误！';
            return false;
        }
        $member = $this->where('member_id', $memberId)->find();
        if (empty($member) || $member['company_id'] != $meetingCompanyInfo['company_id']) {
            $this->error = '您绑定的企业与您的企业不符';
            return false;
        } else { //已绑定直接登录状态
            $meetingModel = new MeetingModel();
            $circleId = $meetingModel->where('meeting_id', $meetingCompanyInfo['meeting_id'])->value('circle_id');
            $circleModel = new CircleModel();
            $circleInfo = $circleModel->where('circle_id', $circleId)->find();
            $data['company_id'] = $meetingCompanyInfo['company_id'];
            $data['company_name'] = $meetingCompanyInfo['company_name'];
            $data['member_tel'] = $data['member_mobile']; //名片电话
            $data['member_id']=$memberId;
            $this->editData($data);
            $loginToken = config('base.login_token');
            $des = str_encode($this->getData('member_id') . $loginToken . $meetingCompanyInfo['company_id']);
            Cookie::set('circleMemberDes', $des, 10 * 30 * 24 * 60 * 60);
            $circleData = [
                'member_id' => $memberId,
                'circle_id' => $circleInfo['circle_id'],
                'circle_name' => $circleInfo['circle_name'],
                'cm_jointime' => time(),
                'is_identity' => 2,
                'cm_thcount' => 0,
                'cm_comcount' => 0,
                'is_recommend' => 0
            ];
            $circleMemberModel = new CircleMemberModel();
            $circleMemberModel->addData($circleData);
            return true;
        }
    }

    /**
     * 登陆
     * @param array $data 登陆参数 ['member_mobile','member_pass','captcha']
     * @return bool
     */
    public function login($member_mobile, $member_pass)
    {
        $member = $this->where(['member_mobile' => $member_mobile, 'member_pass' => md5($member_pass)])->find();
        if (!$member) {
            $this->error = '该用户不存在或者账号密码错误';
            return false;
        } else {
            if ($member->getData('member_status') == 1) {
                $update = ['last_login_ip' => get_client_ip(), 'last_login_time' => time()];
                //$this->save($update);
                $loginToken = config('base.login_token');
                $des = str_encode($member['member_id'] . $loginToken . $member['company_id']);
                Cookie::set('circleMemberDes', $des, 10 * 30 * 24 * 60 * 60);
                return true;
            } else {
                $this->error = '该账户已禁用！';
                return false;
            }
        }
    }

    /**
     * 获取圈子内的通讯录，并且按照拼音分组返回
     * @param int $memberId 会员id
     * @param string $name 搜索姓名
     * @param string $companyName 搜索公司名称
     * @param string $mobile 搜索手机号
     * @param int $circleId 取某个圈子内
     * @return array|bool 是否取出成功以及取出的数组
     */
    public function getContacts($memberId, $name = '', $companyName = '', $mobile = '', $circleId = 0)
    {
        if (empty($memberId)) {
            $this->error = '请先登录';
            return false;
        }
        $mapCircle = ['member_id' => $memberId];
        $memberAttentionModel = new MemberAttentionModel();
        $memberIds = $memberAttentionModel->where($mapCircle)->column('attention_member_id');
        $circleModel = new CircleMemberModel();
        $circleIds = $circleModel->where($mapCircle)->column('circle_id');
        $circleMemberIds = $circleModel->where(['circle_id' => ['in', $circleIds], 'is_identity' => 1])->column('member_id');
        $memberIds = array_merge($memberIds, $circleMemberIds);
        $map = [
            'member_id' => [['<>', $memberId], ['in', $memberIds]],
        ];
        if (!empty($name)) {
            $map['member_truename'] = ['like', "%$name%"];
        }
        if (!empty($companyName)) {
            $model = new CompanyModel();
            $companyIds = $model->where('company_name', 'like', "%$companyName%")->column('company_id');
            $map['company_id'] = ['in', $companyIds];
        }
        if (!empty($mobile)) {
            $map['member_mobile'] = ['like', "%$mobile%"];
        }
        $list = $this->where($map)->select();
        $lists = $list->toArray();
        $pinyin = new Pinyin();
        $pyList = [];
        foreach ($lists as $list) {
            $py = $pinyin->name($list['member_truename']);
            $pyList[substr($py[0], 0, 1)][] = $list;
        }
        return $pyList;
    }
}